﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;
using MyWeb.Business;
using MyWeb.Data;
namespace MyWeb.Admins
{
    public partial class tb_Payment : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["folder"] = "tb_Payment"; BindGrid();
                GetServices();
                GetServicesFilter();
            }
        }


        private void BindGrid()
        {
            grdtb_Payment.DataSource = tb_PaymentService.tb_Payment_GetByAll();
            grdtb_Payment.DataBind();
        }


        protected void btnThem_Click(object sender, EventArgs e)
        {
            pnShow.Visible = false; pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            Session["upload"] = null; Insert = true;
        }

        private void GetServices()
        {

            dllServiceId_Update.Items.Clear();
            ListItem lstAll = new ListItem("Service", "");
            dllServiceId_Update.Items.Add(lstAll);
            var dt = new DataTable();
            dt = tb_ServicesService.tb_Services_GetByAll();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem lstItem = new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Id"].ToString());
                dllServiceId_Update.Items.Add(lstItem);
            }
        }
        private void GetServicesFilter()
        {

            dllServiceId.Items.Clear();
            ListItem lstAll = new ListItem("Service", "");
            dllServiceId.Items.Add(lstAll);
            var dt = new DataTable();
            dt = tb_ServicesService.tb_Services_GetByAll();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem lstItem = new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Id"].ToString());
                dllServiceId.Items.Add(lstItem);
            }
        }
        protected void btnXoa_Click(object sender, EventArgs e) {
            try
            { 
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdtb_Payment.Items.Count; i++) {
                    item = grdtb_Payment.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item) 
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked) {
                            string strId = item.Cells[1].Text; 
                            try {
                                tb_PaymentService.tb_Payment_Delete(strId); 
                            } 
                            catch 
                            {
                            } 
                        }
                    }
                } 
                grdtb_Payment.CurrentPageIndex = 0; BindGrid(); 
            } catch {
            } 
        }
        protected void grdtb_Payment_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            Data.tb_PaymentInfo strCA = new Data.tb_PaymentInfo();
            strCA.Id = e.CommandArgument.ToString();
            if (e.CommandName == "Edit")
            {
                DataTable dt = tb_PaymentService.tb_Payment_GetById(strCA); txtId.Text = dt.Rows[0]["Id"].ToString(); 
                txtUserId.Text = dt.Rows[0]["UserId"].ToString();
                dllServiceId_Update.SelectedValue = dt.Rows[0]["ServiceId"].ToString();
                txtDateTime.Text = dt.Rows[0]["DateTime"].ToString();
                txtAmount.Text = dt.Rows[0]["Amount"].ToString(); 
                txtPhone.Text = dt.Rows[0]["Phone"].ToString();
                if (dt.Rows[0]["Status"].ToString() == "True")
                    ckbStatus.Checked = true;
                else
                    ckbStatus.Checked = false;
                pnUpdate.Visible = true; 
                pnShow.Visible = false;
            }
            else { tb_PaymentService.tb_Payment_Delete(strCA.Id); 
                BindGrid(); 
            }
        }
        protected void lblBack_Click(object sender, EventArgs e) {
            pnUpdate.Visible = false; 
            pnShow.Visible = true; 
            Insert = false; 
        }
        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();
            #region[TestInput]
            if (txtId.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Id không thể trống !");
                txtId.Focus(); return;
            }
            if (txtUserId.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("UserId không thể trống !");
                txtUserId.Focus();
                return;
            }
            if (dllServiceId_Update.SelectedValue.Trim().Equals(""))
            {
                WebMsgBox.Show("ServiceId không thể trống !");
                dllServiceId_Update.Focus();
                return;
            }
            if (txtDateTime.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("DateTime không thể trống !");
                txtDateTime.Focus(); return;
            }
            if (txtAmount.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Amount không thể trống !");
                txtAmount.Focus(); return;
            } if (txtPhone.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Phone không thể trống !");
                txtPhone.Focus(); return;
            }
            
            #endregion
            String status;
            if (ckbStatus.Checked)
                status = "True";
            else
                status = "False";
            Data.tb_PaymentInfo obj = new Data.tb_PaymentInfo();
            obj.Id = txtId.Text; obj.UserId = txtUserId.Text;
            obj.ServiceId = dllServiceId_Update.SelectedValue; 
            obj.DateTime = txtDateTime.Text; 
            obj.Amount = txtAmount.Text; 
            obj.Phone = txtPhone.Text;
            obj.Status = status; 
            if (Insert == true) {
                tb_PaymentService.tb_Payment_Insert(obj); 
            } else { 
                tb_PaymentService.tb_Payment_Update(obj); 
            } BindGrid(); pnShow.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }
        protected void grdtb_Payment_PageIndexChanged(object source, DataGridPageChangedEventArgs e) {
            grdtb_Payment.CurrentPageIndex = e.NewPageIndex; BindGrid(); 
        }
        private void ShowDll()
        {
            var dt = new DataTable();
            dt = tb_ServicesService.tb_Services_GetByAll();
            dllServiceId.DataSource = dt; 
            dllServiceId.DataTextField = ""; 
            dllServiceId.DataValueField = "";
            dllServiceId.DataBind(); 
            dllServiceId_Update.DataSource = dt;
            dllServiceId_Update.DataTextField = "";
            dllServiceId_Update.DataValueField = "";
            dllServiceId_Update.DataBind();
        }
        protected void btnshow_Click(object sender, EventArgs e) {
            string where = " Status=1";
            if (dllServiceId.SelectedValue!="")
            {
                where += "And ServiceId='" + dllServiceId.SelectedValue + "'";
            }
            if (txtFrom.Text!="")
            {
                where += " and [DateTime]>='" + txtFrom.Text+"'";
            }
            if (txtTo.Text!="")
            {
                where += " and DateTime<='" + txtTo.Text+"'";
            }
            grdtb_Payment.DataSource = tb_PaymentService.tb_Payment_GetByTop("", where, "");
            grdtb_Payment.DataBind(); 
        }

        protected void grdtb_Payment_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType; if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll"); if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdtb_Payment.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")"); ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");
                    }
                }
            }
        }
    }
}